<?php

class Dora {
	
	var $config;
	/** 视图数据 **/
	var $view_data = array();
	/** 布局视图数据 **/
	var $layout_view_data = array();
	
	var $template_dir;
	var $layout_dir;
	
	function Dora($config) {
		$this->config = $config;
	}
	
	function database() {
		if( ! isset($this->db) ) {
			require( dirname(__FILE__) . '/libs/db.php' );	
			$this->db = new DB(
				$this->config['db_username'], 
				$this->config['db_password'], 
				$this->config['db_name'], 
				$this->config['db_hostname']);
		}
	}
	
	function lib() {
		$lib_list = func_get_args();
		foreach( $lib_list as $lib ) {
			if( ! isset($this->$lib) ) {
				require( dirname(__FILE__) . '/libs/'. $lib .'.php' );
				$class_name = ucwords($lib);
				$this->$lib = new $class_name();	
			}	
		}
	}

	function helper() {
		$helper_list = func_get_args();
		foreach( $helper_list as $helper  ) {
			require_once( dirname(__FILE__) . '/helpers/'. $helper .'_helper.php' );	
		}
	}
	
	function set($key, $value) {
		$this->view_data[$key] = $value;	
	}
	
	function set_layout_data($key, $value) {
		$this->layout_view_data[$key] = $value;
	} 
	
	function view($viewer, $is_return = false, $layout='layout') {
		$tpl = new Template( $this->template_dir );
		
		$this->view_data['dora'] = $this;
		$tpl->set_vars( $this->view_data );
		
		$this->layout_view_data['layout_body'] = $tpl->fetch( $viewer . '.tpl.php' );
		$this->layout_view_data['dora'] = $this;
		
		$tpl->set_path( $this->layout_dir );
		$tpl->set_vars( $this->layout_view_data, true );
		
		if( $is_return ) {
			return $tpl->fetch(  $layout . '.tpl.php' );
		}
		else {
			echo $tpl->fetch( $layout . '.tpl.php' );
		}
	}
	
	function simple_view($viewer, $is_return = false) {
		$tpl = new Template( $this->template_dir );
		$this->view_data['dora'] = $this;
		$tpl->set_vars( $this->view_data );
		echo $tpl->fetch( $viewer . '.tpl.php' );
	}

}

?>